<?php
include_once("../service/receiptService.php");
include_once("../util/constant.php");

$recId = $_POST["recId"];
$status = $_POST["status"];
$note = $_POST["note"];
$receipt = mysql_fetch_array(findReceiptBy($recId));

if(updateReceipt($recId, $status, $note)){
	/*
	if($status == 5){
		$receipt["REC_STATUS"] == 5 // no edit
		$receipt["REC_STATUS"] != 5 // edit date
	} else {
		$receipt["REC_STATUS"] == 5 // edit null
		$receipt["REC_STATUS"] != 5 // edit null
	}*/
	
	if($status == 5 && $receipt["REC_STATUS"] != 5){
		if(!updateActuallyDeliveryingDate($recId, date('d/m/Y'))){
			echo "<script>window.location.href = '../admin/receipt-detail.php?action=".UPDATE_FAILED.".1&id=".$recId."';</script>";		
		}
	} else if(($status != 5 && $receipt["REC_STATUS"] == 5) || ($status != 5 && $receipt["REC_STATUS"] != 5)){
    	if(!updateActuallyDeliveryingDate($recId, '') && $receipt["REC_ACTUALLYDELIVERYINGDATE"] != ""){
			echo "<script>window.location.href = '../admin/receipt-detail.php?action=".UPDATE_FAILED.".1&id=".$recId."';</script>";		
		}
    }
} else{
	echo "<script>window.location.href = '../admin/receipt-detail.php?action=".UPDATE_FAILED."&id=".$recId."';</script>";
}

echo "<script>window.location.href = '../admin/receipt-detail.php?action=".UPDATE_SUCCESSFULLY."&id=".$recId."';</script>";
?>